package com.wechat.backend.service.impl;

import com.wechat.backend.entity.Message;
import com.wechat.backend.entity.Session;
import com.wechat.backend.repository.MessageRepository;
import com.wechat.backend.repository.SessionRepository;
import com.wechat.backend.repository.UserRepository;
import com.wechat.backend.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageRepository messageRepository;

    @Autowired
    private SessionRepository sessionRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<Message> getHistory(Integer userId, Integer friendId) {
        return messageRepository.findHistory(userId, friendId);
    }

    @Override
    public Map<String, Object> getUnreadCount(Integer userId, Integer friendId) {
        int count = messageRepository.countUnread(userId, friendId);
        Map<String, Object> result = new HashMap<>();
        result.put("count", count);
        return result;
    }

    @Override
    @Transactional
    public Map<String, Object> markAllAsRead(Integer userId, Integer friendId) {
        int updated = messageRepository.markAllAsRead(userId, friendId);
        // 清零会话表未读数
        Session session = sessionRepository.findByUserIdAndFriendId(userId, friendId);
        if (session != null) {
            session.setUnreadCount(0);
            sessionRepository.save(session);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("updated", updated);
        return result;
    }

    @Override
    @Transactional
    public void saveMessage(Integer fromId, Integer toId, String content) {
        // 保存消息
        Message msg = new Message();
        msg.setFromId(fromId);
        msg.setToId(toId);
        msg.setContent(content);
        msg.setStatus("unread");
        msg.setCreateTime(new Date());
        messageRepository.save(msg);

        // 更新发送方会话
        Session fromSession = sessionRepository.findByUserIdAndFriendId(fromId, toId);
        if (fromSession == null) {
            fromSession = new Session();
            fromSession.setUserId(fromId);
            fromSession.setFriendId(toId);
        }
        fromSession.setLastMessage(content);
        fromSession.setLastTime(new Date());
        sessionRepository.save(fromSession);

        // 更新接收方会话
        Session toSession = sessionRepository.findByUserIdAndFriendId(toId, fromId);
        if (toSession == null) {
            toSession = new Session();
            toSession.setUserId(toId);
            toSession.setFriendId(fromId);
            toSession.setUnreadCount(1);
        } else {
            toSession.setUnreadCount(toSession.getUnreadCount() + 1);
        }
        toSession.setLastMessage(content);
        toSession.setLastTime(new Date());
        sessionRepository.save(toSession);
    }
} 